<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="水果名称" prop="fruitsId">
        <el-select
          v-model="queryParams.fruitsId"
          placeholder="请选择水果名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        >
          <el-option
            v-for="fruitsInfo in fruitsInfoList"
            :key="fruitsInfo.id"
            :label="fruitsInfo.fruitsName"
            :value="fruitsInfo.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="进货日期" prop="salesTime">
        <el-date-picker clearable
                        v-model="queryParams.salesTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择进货日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['fruits:FruitsStockInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['fruits:FruitsStockInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fruits:FruitsStockInfo:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['fruits:FruitsStockInfo:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="FruitsStockInfoList" @selection-change="handleSelectionChange"   show-summary :summary-method="getSummaries">
      <el-table-column type="selection" width="80" align="center" />
      <el-table-column type="index" label="序号" align="center"></el-table-column>
      <el-table-column label="水果名称" align="center" prop="fruitsName" />
      <el-table-column prop="unit" label="单位"  align="center" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.unit" :value="scope.row.unit"/>
        </template>
      </el-table-column>
      <el-table-column label="进货日期" align="center" prop="salesTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.salesTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="进货数量" align="center" prop="stockNum" />
      <el-table-column label="进货价" align="center" prop="purchasePrice" />
      <el-table-column label="零售价" align="center" prop="retailPrice" />

      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="更新人名称" align="center">
        <template slot-scope="scope">
          {{
            selectUserDictLabel(
              'userId',
              'nickName',
              scope.row.updateBy
            )
          }}
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['fruits:FruitsStockInfo:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['fruits:FruitsStockInfo:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改水果进货管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="水果名称" prop="fruitsId">
          <el-select
            v-model="form.fruitsId"
            placeholder="请选择水果名称"
            clearable
          >
            <el-option
              v-for="fruitsInfo in fruitsInfoList"
              :key="fruitsInfo.id"
              :label="fruitsInfo.fruitsName"
              :value="fruitsInfo.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="进货日期" prop="salesTime">
          <el-date-picker clearable
                          v-model="form.salesTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择进货日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="进货数量" prop="stockNum">
          <el-input-number v-model="form.stockNum" placeholder="请输入进货数量" :precision="2"></el-input-number>
        </el-form-item>
        <el-form-item label="进货价" prop="purchasePrice">
          <el-input-number v-model="form.purchasePrice" placeholder="请输入进货价" :precision="2"></el-input-number> 元
        </el-form-item>
        <el-form-item label="零售价" prop="retailPrice">
          <el-input-number v-model="form.retailPrice" placeholder="请输入零售价" :precision="2"></el-input-number> 元
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFruitsStockInfo, getFruitsStockInfo, delFruitsStockInfo, addFruitsStockInfo, updateFruitsStockInfo } from "@/api/fruits/FruitsStockInfo";
import { listAll } from '@/api/fruits/fruitsInfo'

export default {
  name: "FruitsStockInfo",
  dicts: ['unit'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 水果进货管理表格数据
      FruitsStockInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fruitsId: null,
        salesTime: null,
        purchasePrice: null,
        retailPrice: null,
        stockNum: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fruitsId: [
          { required: true, message: "水果名称不能为空", trigger: "blur" }
        ],
        salesTime: [
          { required: true, message: "进货日期不能为空", trigger: "blur" }
        ],
        stockNum: [
          { required: true, message: "进货数量不能为空", trigger: "change" }
        ],
        purchasePrice: [
          { required: true, message: "进货价不能为空", trigger: "change" }
        ],
        retailPrice: [
          { required: true, message: "零售价不能为空", trigger: "change" }
        ]
      },
      fruitsInfoList:[],
    };
  },
  created() {
    this.getFruitsInfoList();
    this.getList();
  },
  methods: {
    /** 查询水果进货管理列表 */
    getList() {
      this.loading = true;
      listFruitsStockInfo(this.queryParams).then(response => {
        this.FruitsStockInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getFruitsInfoList()
    {
      listAll().then(response => {
        this.fruitsInfoList = response.rows;
        if(this.fruitsInfoList)
        {
          this.fruitsInfoList.forEach(e=>{
            e.fruitsName = e.fruitsName+"（"+this.selectDictLabel(this.dict.type.unit, e.unit)+"）"
          })
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        fruitsId: null,
        salesTime: null,
        purchasePrice: null,
        retailPrice: null,
        stockNum: null,
        remark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加水果进货管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFruitsStockInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改水果进货管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFruitsStockInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFruitsStockInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除水果进货管理编号为"' + ids + '"的数据项？').then(function() {
        return delFruitsStockInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fruits/FruitsStockInfo/export', {
        ...this.queryParams
      }, `FruitsStockInfo_${new Date().getTime()}.xlsx`)
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = {};
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (column.label === "进货数量") {
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
            // sums[index] += ' kg';
          } else {
            // sums[index] = 'N/A';
          }
        }
      });
      return sums;
    },
  }
};
</script>
